<template>
  <div class="bg-content">
    <div class="section2-list">
      <div
        class="item"
        @click="godetail(item)"
        v-for="(item, index) in alllist"
        :key="index"
      >
        <div class="item-img" v-if="item.image">
          <img :src="item.image" alt="" />
        </div>
        <div class="item-img" v-else>
          <img src="../../../../assets/img/main/moren.jpg" alt="" />
        </div>
        <div class="item-content">
          <div class="title">{{ item.title }}</div>
          <div style="display: flex">
            <div class="icon">
              <div class="icon-img"></div>
              <div class="i-content">{{ item.watchNum }}人</div>
            </div>
            <div class="icon1">
              <div class="icon1-img"></div>
              <div class="i-content1">{{ item.clickNum }}人</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="pupop">
      <van-popup
        v-model="show"
        position="bottom"
        closeable
        close-icon-position="top-right"
        :style="{ height: '40%' }"
      >
        <div class="poptitle">全部频道</div>
        <div class="popmain">
          <div
            class="popitem"
            v-for="(item, index) in navlist"
            :key="index"
            :class="{ active: active == item.id }"
            @click="chooseitem(item.id)"
          >
            {{ item.title }}
          </div>
        </div>
      </van-popup>
    </div>
  </div>
</template>
<script>
import { alljiankangApi } from "@/service/api.js";
import { Toast } from "vant";
import { Dialog } from "vant";
import { Tab, Tabs, Popup, List } from "vant";
import { PullRefresh } from "vant";
export default {
  name: "index",
  data() {
    return {
      active: "",
      show: false,
      active: "",
      apptoken: "",
      newsId: "",
      pageNumber: "1",
      pageSize: "15",
      alllist: [],
      onFooter: 0,
      // onPullDown: true, //下拉加载
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      total: "",
      onFooter: 0,
      onPullDown: true, //下拉加载
      listlength: 1,
      timer: null,
      navlist: [
        {
          title: "推荐",
          id: 0
        },
        {
          title: "儿科",
          id: 1
        },
        {
          title: "妇科",
          id: 2
        },
        {
          title: "皮肤科",
          id: 3
        },
        {
          title: "产科",
          id: 4
        },
        {
          title: "口腔科",
          id: 5
        },
        {
          title: "耳鼻喉科",
          id: 6
        }
      ]
    };
  },
  created() {
    this.apptoken = JSON.parse(localStorage.getItem("apptoken"));
    localStorage.setItem("apptoken1", this.apptoken);
    this.getalljiankang();
  },
  mounted() {
    this.$nextTick(() => {
      window.addEventListener("scroll", this.handleScroll, true);
    });
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll); //  离开页面清除（移除）滚轮滚动事件
  },
  methods: {
    //页面滑到底部需要调用的方法
    handleScroll(e) {
      // 获取滚动条距离顶部的位置
      var that = this;
      var scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      // windowHeight 可视区的高度
      var windowHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      var scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      console.log(scrollTop + windowHeight >= scrollHeight);
      if (scrollTop + windowHeight == scrollHeight) {
        if (that.pageNumber == that.total) {
          window.removeEventListener("scroll", this.handleScroll);
        } else {
          that.pageNumber++;
          that.getalljiankang();
        }
      }
    },
    showPopup() {
      this.show = true;
    },
    chooseitem(id) {
      this.active = id;
    },
    getalljiankang() {
      alljiankangApi({
        apptoken: this.apptoken,
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      }).then(res => {
        console.log(res);
        if (res.code == 3999) {
          Toast.fail({
            message: "apptoken过期"
          });
        }

        this.total = res.data.totalPage;
        if (res.data.list.length != 0) {
          res.data.list.forEach(el => {
            let item = this.alllist.find(ele => ele.id == el.id);
            if (!item) {
              this.alllist.push(el);
            }
          });
        }
        console.log(this.alllist);
      });
    },

    godetail(item) {
      let token = localStorage.getItem("token");
      this.newsId = item.id;
      let pagenumber = JSON.stringify(this.pageNumber);
      localStorage.setItem("pageNumber", pagenumber);
      let newsId = JSON.stringify(this.newsId);
      localStorage.setItem("newsId", newsId);
      this.$router.push({
        name: "jiankangdetail"
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.bg-content {
  width: 100%;
  margin-top: 1.5rem;
}
.poptitle {
  font-size: 18px;
  font-family: PingFang SC;
  font-weight: 500;
  color: #333333;
  line-height: 18px;
  margin-left: 13px;
  margin-top: 15px;
}
.active {
  outline: none;
  color: #fff;
  background-color: #07c6b4;
  border: 1px solid transparent !important;
}
.popmain {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  .popitem {
    padding: 0 15px;
    //  width: 48px;
    border: 1px solid #aaaaaa;
    height: 29px;
    // background: #fff;
    border-radius: 15px;
    text-align: center;
    line-height: 29px;
    font-size: 14px;
    margin-left: 13px;
    margin-top: 15px;
  }
}
.nav {
  width: 100%;
  display: flex;
  .nav-left {
    width: 90%;
    margin-left: 6px;
  }
  .nav-right {
    width: 16px;
    height: 13px;
    margin-top: 3px;
    // background-color: red;
    // margin-top: 15px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.section2-list {
  width: 100%;
  background-color: #fff;
  .item {
    height: 1.5rem;
    width: 95%;
    margin: 0 auto;
    display: flex;
    padding-bottom: 0.15rem;
    .item-img {
      width: 2rem;
      height: 1.3rem;
      background-color: pink;
      border-radius: 10px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .item-content {
      width: 7rem;
      margin-left: 0.15rem;
      .title {
        margin-top: 0.08rem;
        line-height: 0.5rem;
        font-size: 0.38rem;
        font-family: PingFang SC;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: #333333;
      }
      .icon {
        display: flex;
        margin-top: 0.15rem;
        width: 2.5rem;
        .icon-img {
          width: 0.4rem;
          height: 0.3rem;
          background: url(../../../../assets/img/main/yj.png) no-repeat;
          background-size: 100% 100%;
          margin-top: 0.1rem;
        }
        .i-content {
          margin-left: 0.1rem;
          font-size: 0.3rem;
          color: #c2c2c2;
        }
      }
      .icon1 {
        display: flex;
        margin-top: 0.15rem;
        .icon1-img {
          width: 0.3rem;
          height: 0.3rem;
          margin-top: 0.05rem;
          background: url(../../../../assets/img/main/dz.png) no-repeat;
          background-size: cover;
        }
        .i-content1 {
          margin-left: 0.1rem;
          font-size: 0.3rem;
          color: #c2c2c2;
        }
      }
    }
  }
}
</style>
